package com.bootdo.wx.controller;

import com.bootdo.common.utils.*;
import com.bootdo.wx.domain.CmsProblemDO;
import com.bootdo.wx.domain.SalesCouponBagsDO;
import com.bootdo.wx.service.CmsProblemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;
/**
 * 
 * 
 * @author long
 * @email 499626561@qq.com
 * @date 2020-08-06 09:36:48
 */
 
@Controller
@RequestMapping("/wx/cmsProblem")
public class CmsProblemController {
	@Autowired
	private CmsProblemService cmsProblemService;
	
	@GetMapping()
	@RequiresPermissions("wx:cmsProblem:cmsProblem")
	String CmsProblem(){
	    return "wx/cmsProblem/cmsProblem";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wx:cmsProblem:cmsProblem")
	public PageUtils list(@RequestParam Map<String, Object> params){
	    params.put("delFlag","0");
	    params.put("sort","ca_at");
	    params.put("order","desc");
        //查询列表数据
        Query query = new Query(params);
	    if(params.containsKey("name")&&StringUtils.isNotBlank(params.get("name").toString())){
            List<CmsProblemDO> cmsProblemList = cmsProblemService.likeList(query);
            int total = cmsProblemList.size();
            PageUtils pageUtils = new PageUtils(cmsProblemList, total);
            return pageUtils;
		}

		List<CmsProblemDO> cmsProblemList = cmsProblemService.list(query);
		int total = cmsProblemService.count(query);
		PageUtils pageUtils = new PageUtils(cmsProblemList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wx:cmsProblem:add")
	String add(){
	    return "wx/cmsProblem/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wx:cmsProblem:edit")
	String edit(@PathVariable("id") String id,Model model){
		CmsProblemDO cmsProblem = cmsProblemService.get(id);
		model.addAttribute("cmsProblem", cmsProblem);
	    return "wx/cmsProblem/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wx:cmsProblem:add")
	public R save( CmsProblemDO cmsProblem){
	    cmsProblem.setId(UuidUtil.get32UUID());
	    cmsProblem.setDelFlag(0);
	    cmsProblem.setCaAt(new Date());
		if(cmsProblemService.save(cmsProblem)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wx:cmsProblem:edit")
	public R update( CmsProblemDO cmsProblem){
		cmsProblem.setUpAt(new Date());
		cmsProblemService.update(cmsProblem);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wx:cmsProblem:remove")
	public R remove( String id){
		CmsProblemDO cmsProblem = cmsProblemService.get(id);
		cmsProblem.setDelFlag(1);
		cmsProblem.setUpAt(new Date());
		if(cmsProblemService.update(cmsProblem)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wx:cmsProblem:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
	   if(ids.length>0){
			for (String string : ids) {
				CmsProblemDO cmsProblem = cmsProblemService.get(string);
				cmsProblem.setDelFlag(1);
				cmsProblem.setUpAt(new Date());
				cmsProblemService.update(cmsProblem);
			}
		}
	
		return R.ok();
	}


	
}
